package com.imooc.distributedemo.resume.nowcoder;

import java.util.Scanner;
import java.util.regex.Pattern;

public class MatchPasswordSolution {

    private static final String success = "OK";
    private static final String fail = "NG";

    public static void main(String[] args) {
        //String str = "aBc123!!abcc";
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.next();
            boolean isRight = matchPwd(str);
            if(isRight){
                System.out.println(success);
            }else{
                System.out.println(fail);
            }
        }


    }

    /**
     * 密码要求:
     *
     * 1.长度超过8位
     *
     * 2.包括大小写字母.数字.其它符号,以上四种至少三种
     *
     * 3.不能有长度大于2的包含公共元素的子串重复 （注：其他符号不含空格或换行
     * @param str
     * @return true/false 符合规则返回true  不符合返回false
     */
    private static boolean matchPwd(String str) {
        // 判断长度
        if(null == str || "".equals(str)){
            return Boolean.FALSE;
        }

        if(str.length()<8){
            return Boolean.FALSE;
        }
        // 判断符合规则
        boolean isRegexRight = isRegex(str);
        if(!isRegexRight){
            return Boolean.FALSE;
        }

        // 判断子串有重复情况
        boolean isSubstrRepeat = isSubstrRepeat(str,0,3);
        if(isSubstrRepeat){
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }

    private static boolean isSubstrRepeat(String str, int l, int r) {
        if(r>=str.length()){
            return Boolean.FALSE;
        }
        if(str.substring(r).contains(str.substring(1,r))){
            return Boolean.FALSE;
        }else{
            return isSubstrRepeat(str,l+1,r+1);
        }
    }

    private static boolean isRegex(String str) {

        // 一个计数器，判断符合规则的条件
        int counter = 0;
        Pattern p1 = Pattern.compile("[A-Z]");
        if(p1.matcher(str).find()){
            counter++;
        }

        Pattern p2 = Pattern.compile("[a-z]");
        if(p2.matcher(str).find()){
            counter++;
        }

        Pattern p3 = Pattern.compile("[0-9]");
        if(p3.matcher(str).find()){
            counter++;
        }
        Pattern p4 = Pattern.compile("^a-zA-Z0-9");
        if(p4.matcher(str).find()){
            counter++;
        }
        if(counter>=3){
            return Boolean.TRUE;
        }else{
            return Boolean.FALSE;
        }
    }

}
